package controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import pojo.User;

@Controller
//@RestController
public class TestController{
	@RequestMapping(value={"/test1"})
	//@GetMapping
	public String test1(Model model)  {
		model.addAttribute("uname", "John");
		return "suc";
	}
	@RequestMapping(value="/test1",method=RequestMethod.GET)
	//@PostMapping
	public String test2(Model model)  {
		model.addAttribute("uname", "Tom");
		return "suc";
	}
	
	
	//text/html,text/plain,applicaton/json,application/xml,image/jpeg,png...
	//@RequestMapping(value="/test1",headers={"Content-Type=application/json"})
	@RequestMapping(value="/test1",consumes="application/json",produces="application/json")	
	public String test3(Model model)  {
			model.addAttribute("uname", "Test3");
			return "suc";
		}
	
	@RequestMapping(value="/test1",params="id!=1")	
	public String test4(Model model)  {
			model.addAttribute("uname", "Test4");
			return "suc";
	}
	
	@RequestMapping(value="/test2")	
	public String test4(/*@RequestParam("u_name")*/ String uname,int age,@DateTimeFormat(pattern="yyyy-MM-dd") Date birth,Model model)  {
			System.out.println(birth);
			age++;
			model.addAttribute("uname", uname);
			model.addAttribute("age", age);
			return "suc";
	}
	
	@RequestMapping(value="/test3")	
	public String test5(User u,Model model)  {
			System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getBirth());
			return "suc";
	}
	@RequestMapping(value="/test4/")	
	public String test6(@CookieValue(value="JSESSIONID") String key)  {
		System.out.println("key:"+key);	
		return "suc";
	}
	@RequestMapping(value="/users/{uid}/books/{bid}")	
	public String test7(@PathVariable(value="uid") String uid,@PathVariable int bid)  {
		System.out.println("uid:"+uid+"\tbid:"+bid);	
		return "suc";
	}
	@ResponseBody
	@RequestMapping(value="/test5",consumes="application/json")	
	public User test8(@RequestBody User u,Model model)  {
			System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getBirth());
			return u;
	}
	
	
	
	
}





















